package com.pq.service.impl;

import com.pq.api.StorageApi;
import com.pq.entity.OrderDO;
import com.pq.dao.OrderDAO;
import com.pq.exception.BizServiceException;
import com.pq.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author pq
 * @since 2021-06-14
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderDAO, OrderDO> implements OrderService {
    @Resource
    private StorageApi storageApi;

    @GlobalTransactional(rollbackFor = BizServiceException.class)
    @Transactional(rollbackFor = Exception.class)
    public boolean generatorOrder(){
        Long id = 1L;
        OrderDO orderDO = OrderDO.builder().userId(1L).count(1).price(100).commodityId(id).build();
        this.save(orderDO);
        storageApi.subtract(id);
        return true;
    }

    @GlobalTransactional(rollbackFor = BizServiceException.class)
    @Transactional(rollbackFor = Exception.class)
    public boolean generatorOrderError(){
        Long id = 1L;
        OrderDO orderDO = OrderDO.builder().userId(1L).count(1).price(100).commodityId(id).build();
        this.save(orderDO);
        storageApi.subtractError(id);
        return true;
    }
}
